package com.rn_demo3;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.provider.DocumentsContract;

import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.rn_demo3.util.SecretkeyVo;


import org.web3j.crypto.Credentials;
import org.web3j.crypto.ECKeyPair;
import org.web3j.crypto.Keys;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class RNModule extends ReactContextBaseJavaModule {

    public static final String LOG_TAG = "RNModule";
    private static final int PICKFILE_REQUEST_CODE = 369;
    private ReactApplicationContext mContext;

    private final ActivityEventListener mActivityEventListener = new ActivityEventListener() {
        @Override
        public void onActivityResult(Activity activity, int requestCode, int resultCode, @Nullable Intent intent) {

            if(requestCode == PICKFILE_REQUEST_CODE && resultCode == Activity.RESULT_OK){
                Uri uri = null;
                if(intent != null){
                    uri = intent.getData();

                }
            }
        }

        @Override
        public void onNewIntent(Intent intent) {

        }
    };

    public RNModule(ReactApplicationContext reactContext) {
        super(reactContext);
        mContext = reactContext;
        reactContext.addActivityEventListener(mActivityEventListener);
    }

    @NonNull
    @Override
    public String getName() {
        return "RNModule";
    }

    @ReactMethod
    public void openFile(){
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("*/*");

        intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, true);

        getCurrentActivity().startActivityForResult(intent, PICKFILE_REQUEST_CODE);
    }

    @ReactMethod
    public void openAssignFolder(String path){
        File file = new File(path);
        if(null==file || !file.exists()){
            return;
        }
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(file), "file/*");
        try {
//            mContext.startActivity(intent);
            mContext.startActivity(Intent.createChooser(intent,"选择浏览工具"));
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }


    @ReactMethod
    public void createEthAccount(Promise promise){
        try {
            SecretkeyVo secretkeyVo = createAccount();

            WritableMap map = Arguments.createMap();
            map.putString("address",secretkeyVo.getAddress());
            map.putString("privateKey",secretkeyVo.getPrivateKey());
            map.putString("publicKey",secretkeyVo.getPublickey());
            promise.resolve(map);
        } catch (InvalidAlgorithmParameterException e) {
            Throwable throwable = new Throwable(e.getMessage());
            promise.reject(throwable);
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            Throwable throwable = new Throwable(e.getMessage());
            promise.reject(throwable);
            throw new RuntimeException(e);
        } catch (NoSuchProviderException e) {
            Throwable throwable = new Throwable(e.getMessage());
            promise.reject(throwable);
            throw new RuntimeException(e);
        }

    }

    /**
     * 创建账户
     * @return
     * @throws InvalidAlgorithmParameterException
     * @throws NoSuchAlgorithmException
     * @throws NoSuchProviderException
     */
    public  SecretkeyVo createAccount() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException {
        ECKeyPair keyPair = Keys.createEcKeyPair();
        BigInteger publicKey = keyPair.getPublicKey();
        BigInteger privateKey = keyPair.getPrivateKey();
        Credentials credentials = Credentials.create(new ECKeyPair(privateKey, publicKey));
        String address = credentials.getAddress();
        return new SecretkeyVo(privateKey.toString(16),"0x04" + publicKey.toString(16), address);
    }

}



